package com.components.project

import android.app.Application
import android.os.Handler
import android.util.Log
import com.components.base.utils.CommUtils
import com.components.cachewebview.ResourceInterceptor
import com.components.cachewebview.WebViewCacheInterceptor
import com.components.cachewebview.WebViewCacheInterceptorInst
import com.components.cachewebview.config.CacheExtensionConfig
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.TbsDownloader
import com.tencent.smtt.sdk.TbsListener
import java.io.File

/**
 * @Author LuoKun
 * @Des
 */
class App : Application() {

    companion object {
        @Volatile
        private var instance: App? = null

        fun getInstance(): App? {
            instance ?: synchronized(this) {
                instance ?: App().also {
                    instance = it
                }
            }
            return instance
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        //CommUtil初始化
        CommUtils.init(this, Handler(), android.os.Process.myTid())
        val builder= WebViewCacheInterceptor.Companion.Builder(this)
        builder.setCachePath(File(this.cacheDir,"cache_path_name"))
            .setDynamicCachePath(File(this.cacheDir,"dynamic_webview_cache"))
            .setCacheSize(1024*1024*100)
            .setConnectTimeoutSecond(20)
            .setReadTimeoutSecond(20)

        val extension = CacheExtensionConfig
        extension.addExtension("json").removeExtension("swf")
        builder.setCacheExtensionConfig(extension)
        builder.setDebug(true)
        builder.setResourceInterceptor(object :ResourceInterceptor{
            override fun interceptor(url: String): Boolean {
                return true
            }
        })
        WebViewCacheInterceptorInst.getInstance()?.init(builder)

        //非wifi情况下，主动下载x5内核
        QbSdk.setDownloadWithoutWifi(true)
        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核
        QbSdk.initX5Environment(this, object : QbSdk.PreInitCallback {
            override fun onCoreInitFinished() {

            }

            override fun onViewInitFinished(p0: Boolean) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核
                Log.e("model", "X5内核预加载成功+$p0")
                onViewInitFinished1(p0)
            }
        })
        //下载X5内核监听
        QbSdk.setTbsListener(object : TbsListener {
            override fun onDownloadFinish(p0: Int) {
                Log.e("model", "onDownloadFinish$p0")
            }

            override fun onInstallFinish(p0: Int) {
                Log.e("model", "onInstallFinish$p0")
            }

            override fun onDownloadProgress(p0: Int) {
                Log.e("model", "onDownloadProgress$p0")
            }
        })
    }

    fun onViewInitFinished1(boolean: Boolean) {
        if (!boolean && !QbSdk.getIsSysWebViewForcedByOuter()) {
            QbSdk.reset(this)
            TbsDownloader.startDownload(this)
        }
    }

}